// iViewComponent/picker/index.js
Component({
    behaviors: ['wx://form-field'],
    /**
     * 组件的属性列表
     */
    properties: {
        title: {
            type: String
        },
        mode: {
            type: String
        },
        value: {
            type: String,
            value: '',
            observer: 'changeValue'
        },
        start: {
            type: String
        },
        end: {
            type: String
        },
        disable: {
            type: Boolean
        },
        placeholder: {
            type: String
        },
        range: {
            type: Array
        },
        key: {
            type: String
        },
        rangeValue: {
            type: Object
        },
        rangeShow: {
            type: String
        }

    },

    /**
     * 组件的初始数据
     */
    data: {

    },

    /**
     * 组件的方法列表
     */
    methods: {
        handleDateTimePickerChange: function(e) {
            console.log('PickerChange', e.detail)
            this.setData({
                value: e.detail.value
            })

            // this.changeValue(e.detail.value)
        },
        bindRangePickerChange: function(e) {
            console.log('bindRangePickerChange', e.detail.value, this.data.range[e.detail.value].id)
            let _value = this.data.range[e.detail.value].id
            this.setData({
                    value: _value
                })
                // this.changeValue(e.detail.value)
        },
        changeValue(value = this.data.value) {
            console.log('changeValue', value)
                // let _value
            let param = { key: this.data.name, value: value }
            if (this.data.mode === 'selector') {
                let p = this.data.range.find((item) => item.id == value)
                if (p) {
                    this.setData({
                        rangeShow: p.name
                    })
                }

            }
            this.triggerEvent('change', param);
            console.log('changeValue', 'final', param)
        }
    },
    observers: {
        // 'value': function(value) {
        //     console.log('changeValue!!!', value)
        //     if (this.data.mode === 'selector') {
        //         let p = this.data.range.find((item, index) => item.id == value)
        //         console.log('p', p)
        //         if (p) {
        //             this.setData({
        //                 rangeShow: p.name || ''
        //             })
        //         }
        //     }
        //     let param = { key: this.data.name, value: value }
        //     this.triggerEvent('change', param);
        // }
    }
})